package {
	import net.badimon.five3D.display.DynamicText3D;
	import net.badimon.five3D.display.Scene3D;
	import net.badimon.five3D.display.Shape3D;
	import net.badimon.five3D.display.Sprite3D;
	import net.badimon.five3D.typography.HelveticaBold;
	import net.badimon.five3D.utils.DrawingUtils;

	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[SWF(width="600", height="400", frameRate="71", backgroundColor="#FFFFFF")]

	public class Application extends Sprite {

		private var __scene:Scene3D;
		private var __sign:Sprite3D;
		private var __star:Shape3D;
		private var __hi:DynamicText3D;
		private var __world:DynamicText3D;

		public function Application() {
			
			// We define the Stage scale mode.
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			// We create a new Scene3D named "scene", center it and add it to the display list.
			__scene = new Scene3D();
			__scene.x = 300;
			__scene.y = 200;
			addChild(__scene);
			
			// We create a new Sprite3D named "sign", draw a rounded rectangle inside and add it to the "scene" display list.
			__sign = new Sprite3D();
			__sign.graphics3D.beginFill(0x000000);
			__sign.graphics3D.drawRoundRect(-150, -150, 300, 300, 40, 40);
			__sign.graphics3D.endFill();
			__scene.addChild(__sign);
			
			// We create a new Shape3D named "star", draw a star inside, place it and add it to the "sign" display list.
			__star = new Shape3D();
			DrawingUtils.star(__star.graphics3D, 20, 60, 50, 0, 0xD7006C);
			__star.x = 120;
			__star.y = -80;
			__sign.addChild(__star);
			
			// We create a new DynamicText3D named "hi", modify its properties, place it and add it to the "sign" display list.
			__hi = new DynamicText3D(HelveticaBold.instance);
			__hi.size = 50;
			__hi.color = 0xFFFFFF;
			__hi.text = "Hi!";
			__hi.x = 88;
			__hi.y = -110;
			__sign.addChild(__hi);
			
			// We create a new DynamicText3D named "world", modify its properties, place it and add it to the "sign" display list.
			__world = new DynamicText3D(HelveticaBold.instance);
			__world.size = 80;
			__world.color = 0x666666;
			__world.text = "World";
			__world.x = -112;
			__world.y = -34;
			__sign.addChild(__world);
			
			// We attribute a random value to the rotations on the X, Y and Z axes of the "sign".
			__sign.rotationX = Math.random() * 100 - 50;
			__sign.rotationY = Math.random() * 100 - 50;
			__sign.rotationZ = Math.random() * 100 - 50;
			
			// We register the class Main as a listener for the "enterFrame" event of the "star".
			__star.addEventListener(Event.ENTER_FRAME, starEnterFrameHandler);
			
			// We register the class Main as a listener for the "click" mouse event of the "sign" and modify some of its mouse-related properties.
			__sign.addEventListener(MouseEvent.CLICK, signClickHandler);
			__sign.mouseChildren = false;
			__sign.buttonMode = true;
		}

		private function starEnterFrameHandler(event:Event):void {
			// We rotate the "star".
			__star.rotationZ++;
		}

		private function signClickHandler(event:MouseEvent):void {
			// We attribute a new random value to the rotations on the X, Y and Z axes of the "sign".
			__sign.rotationX = Math.random() * 100 - 50;
			__sign.rotationY = Math.random() * 100 - 50;
			__sign.rotationZ = Math.random() * 100 - 50;
		}
	}
}